import {get,add} from "../../until/db.js";
import { multiUpload } from "../../until/tools.js";
const app=getApp()
Page({
  data:{
    list:[],
    files:[]
  },
  async onLoad(){
    // console.log(app);
    //读取菜谱分类
    var result = await get("class")
    // console.log(result);
    this.setData({
      list:result.data
    })
  },
  selectImage(e){
    // console.log(e);
    var arr = e.detail.tempFilePaths
    var files = arr.map(item=>{
      return {url:item}
    })
    this.setData({
      files
    })
  },
  async mySubmit(e){
    wx.showLoading({
      title: '正在提交',
      mask:true
    })
    var arr = this.data.files.map(item=>{
      return item.url
    })
    var result = await multiUpload(arr)
    // console.log(result);
    //获取图片云端地址组成的数组
    var image = result.map(item=>{
      return item.fileID
    })
    // console.log(images);
    console.log(e);
    var data = e.detail.value;
    data.image = image;
    data.avatarUrl = app.globalData.userInfo.avatarUrl;
    data.nickName =app.globalData.userInfo.nickName;
    data.views=0;
    data.likes=0;
    data.addtime = new Date().getTime()
    data.status=1

    var result = await add("menu",data).catch(err=>{
      console.log(err);
    })

    wx.hideLoading();
    wx.showToast({
      title: '添加成功',
      duration:2000
    })
    setTimeout(()=>{
      wx.navigateBack()
    },1500)
  }
})